Go Back

About

YouTube Auto Quality automatically applies your preferred video resolution and saves it between browser sessions. A simple popup lets you choose the quality, while the extension detects navigation between videos. My focus was connecting its settings to YouTube’s player and handling updates without a full page reload.

Project Info

Role: Sole developer

Team Size: 1

Time Frame: 4 days

Platform: Chrome

Tags:

JavaScriptHTML

Introduction

My main focus for this project was removing the need to manually select the video quality each time a new video starts. YouTube already includes quality controls, but I wanted a way to save a preferred resolution and apply it when moving between videos.

I kept the interface simple, using a single dropdown in the extension’s popup. The user selects a resolution, and the scripts handle passing that preference to the player. This kept the scope focused on the main functionality, while leaving most of the complexity behind the interface.

The more interesting part was handling when that setting should be applied. Navigating to another video does not necessarily reload the page, and finding the player does not mean its quality options are ready yet.

Showcase

The video below shows the extension in action, from selecting a preferred resolution in the popup to applying that setting when navigating to another video.

Connecting the Extension to the Player

One of the main technical challenges was connecting the extension’s saved setting to the JavaScript running on YouTube’s page. Chrome content scripts run in an isolated environment, so accessing the page’s elements does not automatically give the script access to the player’s JavaScript methods.

To handle this, I separated the functionality into three scripts. The popup saves the selected quality, the content script reads that preference, and an injected script interacts with the player. This gives each script a specific responsibility without putting the settings interface and player logic in the same place.

The content script passes the preference through a custom UpdateQuality event. It also listens for an InjectionReady event from the injected script, which signals when to send the initial setting. When the stored preference changes, the same update function sends the new value.

function UpdateInjectedQuality() {
    chrome.storage.sync.get('quality', (data) => {
        const quality = data.quality || 'hd1080';
        window.dispatchEvent(new CustomEvent('UpdateQuality', { detail: quality }));
    });
}

chrome.storage.onChanged.addListener(UpdateInjectedQuality);
window.addEventListener('InjectionReady', UpdateInjectedQuality);

I use chrome.storage.sync to save the selected resolution, with 1080p as the default. This preserves the preference between sessions and allows Chrome to sync it when browser sync is enabled. The extension does not require a separate backend to manage the setting.

Handling Navigation Between Videos

YouTube updates its page as the user moves between videos, so running the quality logic only when the extension first loads would miss later navigation. I needed a way to detect when a different video had been opened within the same page.

I built this around a MutationObserver, which checks whether the URL has changed when elements are added or removed from the document. If the URL differs from the previous one, the script calls HandleNewVideoLoad to check the video ID.

Tracking the current video ID lets the script distinguish a new video from other changes on the page. When it detects a different video on a standard watch URL, it updates the stored ID and schedules the quality selection after a short delay. Leaving that watch-page URL pattern clears the ID so the next video can be handled again.

let lastUrl = location.href;
const observer = new MutationObserver(() => {
    if (location.href !== lastUrl) {
        lastUrl = location.href;
        HandleNewVideoLoad();
    }
});

observer.observe(document, { subtree: true, childList: true });

Selecting an Available Quality

Another thing I needed to account for was that not every video supports the selected resolution. Requesting 4K would not be useful if the video was only available in 1080p.

Before changing the quality, the script checks the options reported by the player. If the preferred resolution is available, it selects that option. Otherwise, it uses the first entry in the available-quality list, which the implementation treats as the highest available quality. This means the preference is an exact choice when supported, rather than a maximum resolution limit.

The player can also exist before its quality information is ready. When the available-quality list is empty, or the current quality is unknown, the selection function waits before trying again. If the player already reports the desired quality, it returns without requesting another change.

const bestOption = available.includes(targetQuality) 
                    ? targetQuality 
                    : available[0]; // youtube sorts from best to worst

player.setPlaybackQualityRange(bestOption, bestOption);
player.setPlaybackQuality(bestOption);
VerifyQuality(bestOption, player);

What I Learned

This project helped me work with the separation between a browser extension and the page it interacts with. Even though the interface only contains one setting, applying that setting involves storage, communication between scripts, and changes to the video player’s state.

The main takeaway for me was that timing is part of the functionality. A page can be visible while the player is still loading, and a different video can appear without starting a new page load. Separating navigation detection, quality selection, and verification made those different situations easier to reason about.

There is also room to improve how the extension handles delayed work. A future improvement would be to cancel pending retries when the user switches videos and put a limit on how long the selection function keeps waiting for quality information. That would make the behavior more predictable when navigating quickly or when the player does not become ready.

Building this extension was a useful way to work on a small feature with several moving parts. Keeping the interface focused gave me more room to work on the communication and playback logic behind it.

Check out the repository on Github , or get the extension from the Chrome Web Store .

Developed by Shahin Mohseni, using Svelte. Visual direction inspired by the portfolio of Tomas Wallin.